<template>
</template>
<script>
import {
  BIND_SHOP,
  SHOP_INFO
} from "@/api/shop";

export default {
  props: {
    teamId: [String, Number],
    tid: [String, Number],
    cid: [String, Number],
    redirect: [String],
    code: [String]
  },

  created() {
    this.$toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true, // 禁用背景点击
      message: '加载中'
    });
    
    //兼容老系统链接，不做判断，直接进主页面
    if (this.tid) {
      this.$router.replace(this.redirect);
      return;
    }

    //团长只能进入自己的店
    if (this.user.isteam === 3) {
      this.$router.replace(this.redirect);
      return;
    }
    
    let shopId = this.user.upshopTid;
    this.$toast.clear();
    this.user.upshopTid = this.teamId;
    this.user.centreId = this.cid;
    this.saveUser();

    if (!shopId || this.user.isteam !== 3) {
      this.bindShop();
    } else {
      this.refreshShop();
    }
  },

  methods: {
    bindShop() {
      this.$reqPost(BIND_SHOP, {
        memberId: this.user.id,
        teamId: this.teamId,
        invitaCode: this.code
      }).then(res => {
        this.refreshShop();
      });
    },

    refreshShop() {
      this.$reqGet(SHOP_INFO, {
        id: this.user.upshopTid
      }).then(res => {
        const shop = res.data.data;
        this.saveShop(shop);
        if (this.user.centreId !== shop.centreId) {
          this.user.centreId = shop.centreId;
          this.saveUser();
        }
        this.$router.replace(this.redirect);
      });
    }
  }
}
</script>

